/*
 * $HeadURL: $
 * $Id: $
 * Copyright (c) 2011 by Ericsson, all rights reserved.
 */
package org.jample.xml;

import java.io.IOException;
import java.io.StringWriter;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

/**
 * @author eccicce
 * @version $Revision: $
 * 
 */
public class XmlUtils {

    /** */
    private static final String DEFAULT_ENCODING = "UTF-8";
    /** Revision of the class */
    public static final String _REV_ID_ = "$Revision: $";

    public static String formatXml(String str) {
        return formatXml(str, DEFAULT_ENCODING);
    }

    public static String formatXml(String str, String encoding) {
        try {
            Document document = DocumentHelper.parseText(str);
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding(encoding);
            StringWriter writer = new StringWriter();
            XMLWriter xmlWriter = new XMLWriter(writer, format);
            xmlWriter.write(document);
            xmlWriter.close();
            return writer.toString();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
